package cn.chitanda.weather.ui.CityList


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import cn.chitanda.weather.R
import cn.chitanda.weather.ui.PlaceViewModel
import com.jaeger.library.StatusBarUtil
import kotlinx.android.synthetic.main.fragment_city_list.*

/**
 * A simple [Fragment] subclass.
 */
class CityListFragment : Fragment() {
    private val placeViewmodel by lazy {
        ViewModelProvider(
            this,
            ViewModelProvider.AndroidViewModelFactory(requireActivity().application)
        ).get(PlaceViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_city_list, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        StatusBarUtil.setLightMode(requireActivity())
        toAddCityActionButton.setOnClickListener {
            findNavController().navigate(R.id.action_cityListFragment_to_addCityFragment)
        }
        val cityListAdapter = CityListAdapter()
        citylistRecycler.apply {
            adapter = cityListAdapter
            layoutManager = LinearLayoutManager(requireContext())
        }


        placeViewmodel.cityList.observe(this, Observer {
            cityListAdapter.submitList(it)
        })

        cityList_TB.setNavigationOnClickListener { findNavController().navigateUp() }
    }

}
